<?php
include('../confing/common.php');
$redis = new Redis();
$redis->connect("127.0.0.1", "6379");

while(true) {
    try {
               // 修改查询逻辑，只查询待更新状态的订单
        $sl = $DB->count("SELECT count(*) FROM qingka_wangke_order WHERE status = '待更新' or status = '' or status = '获取中'  LIMIT 200"); 
        echo "查询到 " . $sl . " 条待更新订单\n";
        
        $rows = $DB->query("SELECT * FROM qingka_wangke_order WHERE status = '待更新' or status = '' or status = '获取中'  LIMIT 200");

        
        // 如果没有待更新的订单，等待60秒后继续检查
        if(!$rows || $sl == 0) {
            echo "没有待更新的订单，等待60秒...\n";
            sleep(60);
            continue;
        }

        // 批量处理数组
        $success_updates = array();
        $failed_updates = array();
        
        // 先收集所有订单的处理结果
        foreach($rows as $row) {
            echo "正在处理订单ID：" . $row['oid'] . "\n";
            $result = processCx($row['oid']);
            
            if(is_array($result) && count($result) > 0) {
                echo "订单 {$row['oid']} 获取数据成功\n";
                $success_updates[] = array(
                    'oid' => $row['oid'],
                    'data' => $result[0]
                );
            } else {
                echo "订单 {$row['oid']} 获取数据失败\n";
                $failed_updates[] = $row['oid'];
            }
            // 短暂休眠以避免API限制
            usleep(200000); // 休眠0.2秒
        }

        // 批量处理成功的更新
        if(!empty($success_updates)) {
            $success_cases = array();
            $yid_cases = array();
            $kcks_cases = array();
            $kcjs_cases = array();
            $ksks_cases = array();
            $ksjs_cases = array();
            $process_cases = array();
            $remarks_cases = array();
            
            foreach($success_updates as $update) {
                $success_cases[] = "WHEN '{$update['oid']}' THEN '{$update['data']['status_text']}'";
                $yid_cases[] = "WHEN '{$update['oid']}' THEN '{$update['data']['yid']}'";
                $kcks_cases[] = "WHEN '{$update['oid']}' THEN '{$update['data']['kcks']}'";
                $kcjs_cases[] = "WHEN '{$update['oid']}' THEN '{$update['data']['kcjs']}'";
                $ksks_cases[] = "WHEN '{$update['oid']}' THEN '{$update['data']['ksks']}'";
                $ksjs_cases[] = "WHEN '{$update['oid']}' THEN '{$update['data']['ksjs']}'";
                $process_cases[] = "WHEN '{$update['oid']}' THEN '{$update['data']['process']}'";
                $remarks_cases[] = "WHEN '{$update['oid']}' THEN '{$update['data']['remarks']}'";
            }
            
            $oids = array_column($success_updates, 'oid');
            $oids_str = "'" . implode("','", $oids) . "'";
            
            // 使用CASE语句进行批量更新
            $sql = "UPDATE qingka_wangke_order SET 
                status = CASE oid " . implode(' ', $success_cases) . " END,
                yid = CASE oid " . implode(' ', $yid_cases) . " END,
                courseStartTime = CASE oid " . implode(' ', $kcks_cases) . " END,
                courseEndTime = CASE oid " . implode(' ', $kcjs_cases) . " END,
                examStartTime = CASE oid " . implode(' ', $ksks_cases) . " END,
                examEndTime = CASE oid " . implode(' ', $ksjs_cases) . " END,
                process = CASE oid " . implode(' ', $process_cases) . " END,
                remarks = CASE oid " . implode(' ', $remarks_cases) . " END
                WHERE oid IN ({$oids_str})";
                
            echo "执行SQL更新...\n";
            $update_result = $DB->query($sql);
            if($update_result) {
                echo "批量同步成功，成功更新 " . count($success_updates) . " 个订单\n";
                foreach($success_updates as $update) {
                    echo "订单ID：{$update['oid']} 更新成功\n";
                }
            } else {
                echo "SQL执行失败\n";
                echo "SQL语句：" . $sql . "\n";
            }
        }

        // 批量处理失败的更新
        if(!empty($failed_updates)) {
            $failed_oids = "'" . implode("','", $failed_updates) . "'";
            $DB->query("UPDATE qingka_wangke_order SET status='同步失败' WHERE oid IN ({$failed_oids})");
            echo "同步失败的订单数量：" . count($failed_updates) . "\n";
            foreach($failed_updates as $failed_oid) {
                echo "订单ID：{$failed_oid} 更新失败\n";
            }
        }

        flush();
        ob_flush();
        
        // 处理完一批后等待30秒
        echo "等待30秒后处理下一批...\n";
        sleep(30);
        
    } catch (Exception $e) {
        echo "发生错误：" . $e->getMessage() . "\n";
        sleep(60); // 发生错误时等待60秒
    }
}
?> 